package config;


import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.servlet.config.annotation.*;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;

@Configuration
@ComponentScan(basePackages={"com.*"})
//@ComponentScan(basePackages={"com.*"},
//		includeFilters={@ComponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class})},useDefaultFilters=false)
@Import({ JdbcConfig.class,CacheConfig.class,FileUploadConfig.class,Log4jConfig.class,ShiroConfig.class, RedisConfig.class})
//@Import({ JdbcConfig.class, WebConfig.class,CacheConfig.class,RedisConfig.class,TransactionConfig.class,MyBatisConfig.class,FileUploadConfig.class,ShiroConfig.class })
@EnableWebMvc
//@EnableCaching
//@EnableScheduling
//@EnableTransactionManagement
public class SpringConfiguration extends WebMvcConfigurerAdapter {

    @Bean("propertyConfigurer")
    public PropertyPlaceholderConfigurer getPropertyPalce(){
        PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = 	new PropertyPlaceholderConfigurer();
        ClassPathResource[]  classPathResources = {new ClassPathResource("redis.properties"),new ClassPathResource("c3p0.properties"),
                new ClassPathResource("jdbc.properties"),new ClassPathResource("log4j.properties")};
        propertyPlaceholderConfigurer.setLocations(classPathResources);
        return propertyPlaceholderConfigurer;
    }

    /**
     * 对thymeleaf的视图解析器，解析到webapp下的html目录下查找对应的页面
     * @return
     */
    @Bean
    public ThymeleafViewResolver viewResolverThymeLeaf() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setCharacterEncoding("utf-8");
        viewResolver.setOrder(1);
        viewResolver.setViewNames(new String[]{"html/*", "vue/*","jsps/*","templates/*"});
        return viewResolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        // templateEngine
        return templateEngine;
    }


    @Bean
    //@ConfigurationProperties(prefix ="spring.thymeleaf")
    public ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setPrefix("classpath:/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setCharacterEncoding("utf-8");
        templateResolver.setCacheable(false);

        return templateResolver;
    }

    // 静态资源访问
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/").setCachePeriod(31556926);
        registry.addResourceHandler("/upload/**").addResourceLocations("classpath:/upload/").setCachePeriod(31556926);
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/").setCachePeriod(31556926);
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");


        String os = System.getProperty("os.name");

        if (os.toLowerCase().startsWith("win")) {  //如果是Windows系统
            registry.addResourceHandler("/smallapple/**")
                    // /apple/**表示在磁盘apple目录下的所有资源会被解析为以下的路径
                    .addResourceLocations("file:G:/itemsource/smallapple/") //媒体资源
                    .addResourceLocations("classpath:/META-INF/resources/");  //swagger2页面
        } else {  //linux 和mac
            registry.addResourceHandler("/smallapple/**")
                    .addResourceLocations("file:/resources/smallapple/")   //媒体资源
                    .addResourceLocations("classpath:/META-INF/resources/");  //swagger2页面;
        }
    }


//    2.x对放静态资源没有作用
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        InterceptorRegistration  addInterceptor = 	registry.addInterceptor(new ParamNullInterceptor() {
//        });
//
////        addInterceptor.excludePathPatterns("/tologin");
//        addInterceptor.excludePathPatterns("/static/css/**", "/static/js/**", "/static/**");
//        // 拦截配置
//        //addInterceptor.addPathPatterns("/toLogin");
//    }



}
